// pages/mine/mine.js
var app = getApp();
var util = require("../../utils/util.js");

Page({
    //分享
    onShareAppMessage: function () { },
    /**
     * 页面的初始数据
     */
    data: {
        height: 0,
        userImg: "",
        isShouQuan: false,
        number: ""
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var system = wx.getSystemInfoSync();
        this.setData({
            height: system.screenHeight * 0.33,
            isShouQuan: app.globalData.isShouQuan,
        });
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var userInfo = wx.getStorageSync("userInfo");
        if(!!userInfo){
            userInfo = JSON.parse(userInfo);
            this.setData({
                userInfo: userInfo
            });
        }
        this.getAttention();
    },
    //收取获取用户信息
    getUserInfo: function (res) {
        var detail = res.detail;
        var flag = detail.errMsg;
        var userInfo = this.data.userInfo;
        var that = this;
        if (flag == "getUserInfo:ok") {
            app.globalData.isShouQuan = true;
            this.setData({
                isShouQuan: true
            });
            wx.login({
                success: function (res1) {
                    if (res1.code) {
                        var code = res1.code;
                        util.wxAjax("admin/login", "POST", {
                            "username": detail.userInfo.nickName,
                            "sex": detail.userInfo.gender,
                            "headimgurl": detail.userInfo.avatarUrl,
                            "openid":code
                        }, function (res) {
                            var data = res.data;
                            if (data.result != 200 || data.result != "200"){
                                this.getAttention();
                                wx.showToast({
                                    title: data.msg,
                                    icon:"none"
                                })
                                return false;
                            }
                            if(!!data.data){
                                wx.setStorageSync("userInfo", JSON.stringify(data.data));
                                that.setData({
                                    userInfo: data.data
                                });
                            }
                        });
                    }
                }
            });
        } else {
            app.globalData.isShouQuan = false;
            wx.showToast({
                title: '用户拒绝授权！',
                icon: "none"
            })
        }
    },
    //获取我关注的人和关注我的人
    getAttention:function(){
        var that = this;
        var userInfo = JSON.parse(wx.getStorageSync("userInfo"));
        util.wxAjax("admin/getFansFollowCount","post",{
            adminid: userInfo.smAdmin.id
        },function(res){
            var data = res.data;
            that.setData({
                number:data.data
            });
        });
    }
})